//分享到群复活界面

import GameCtr from "../../Controller/GameCtr";
import WXCtr from "../../Controller/WXCtr";
import Http from "../../Common/Http";
import UserManager from "../../Common/UserManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Revive extends cc.Component {

    @property(cc.Node)
    ndRevive: cc.Node = null;                               //复活界面根节点
    @property(cc.Label)
    lbScore: cc.Label = null;                               //分数
    @property(cc.Node)
    ndTip: cc.Node = null;                                  //提示文字节点
    @property(cc.Label)
    lbCountDown: cc.Label = null;                           //倒计时
    @property(cc.Node)
    ndBtns: cc.Node = null;                                 //所有按钮节点
    @property(cc.Node)
    ndSaveTip: cc.Node = null;                              //保存图片提示弹窗
    @property(cc.Sprite)
    sprExtension: cc.Sprite = null;                         //推广图片

    @property(cc.Label)
    selflabel: cc.Label = null;


    private countDownNum = 30000;
    private selfGoldCount = 0;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        GameCtr.getInstance().setRevive(this);
        this.ndTip.runAction(cc.fadeOut(0));
    }

    start() {
        // WXCtr.onShow(() => { this.countDown(); });
        // this.getUserInfo();
        // this.initBtns();
    }

    onDestroy() {
        // WXCtr.offShow();
    }

    initBtns() {
        if (GameCtr.gameOverSwitchsInfo == undefined || GameCtr.gameOverSwitchsInfo == null) {
            return;
        }
        let btn;
        btn = this.ndBtns.getChildByName("btnRevive");
        if (GameCtr.gameOverSwitchsInfo.revive_switch == 1) {
           // btn.active = true;
        }
        btn = this.ndBtns.getChildByName("btnAd");
        if (GameCtr.gameOverSwitchsInfo.advert_switch == 1) {
            btn.active = true;
        }
        btn = this.ndBtns.getChildByName("btnFriend");
        if (GameCtr.gameOverSwitchsInfo.friend_switch == 1) {
          //  btn.active = true;
        }
        btn = this.ndBtns.getChildByName("btnGroup");
        if (GameCtr.gameOverSwitchsInfo.group_switch == 1) {
          //  btn.active = true;
        }
        btn = this.ndBtns.getChildByName("btnFriends");
        if (GameCtr.gameOverSwitchsInfo.circle_switch == 1 && GameCtr.friendsCircleImg != "") {
          //  btn.active = true;
        }
    }

    getUserInfo() {
        Http.send({
            url: Http.UrlConfig.GET_USERINFO,
            success: (resp) => {
                console.log("get self UserInfo response == ", resp);
                this.selfGoldCount = resp.data.gold_amount;
                let btn = this.ndBtns.getChildByName("btnRevive");
                let lb = btn.getChildByName("lbGoldNum").getComponent(cc.Label);
                this.selflabel.string = "" + this.selfGoldCount;
                lb.string = "-" + GameCtr.gameOverSwitchsInfo.revive_gold[GameCtr.reviveTimes - 1];//+ "/" + this.selfGoldCount;
            },
            data: {
                user_id: UserManager.user_id
            }
        });
    }

    //显示分数
    showScore(score: number) {
        this.lbScore.string = score + "";
        this.countDown();
    }

    //倒计时
    countDown() {
        console.log("倒计时！！！！！！");
        if (!this.ndRevive.active) return;
        if (this.countDownNum < 0) {
            this.cancel();
            return;
        }
        this.lbCountDown.string = this.countDownNum + "";
        this.countDownNum--;
        this.scheduleOnce(() => { this.countDown(); }, 1.0);
    }

    //复活
    reviveByGold() {
        Http.send({
            url: Http.UrlConfig.REVIVE,
            success: (resp) => {
                if (resp.code == 200) {
                    console.log("复活成功", resp);
                    GameCtr.reviveTimes++;
                    GameCtr.revive();
                }
            },
            data: {
                user_id: UserManager.user_id,
                times: GameCtr.reviveTimes                              //第几次复活
            }
        });
    }

    //分享到群
    shareToGroup() {
        WXCtr.share(true);
        // this.unscheduleAllCallbacks();
    }

    //朋友圈
    friendsCircle() {
        console.log("加载朋友圈图片", GameCtr.friendsCircleImg);
        this.sprExtension.node.active = true;
        GameCtr.loadImg(this.sprExtension, GameCtr.friendsCircleImg);
    }

    clickSprExtension() {
        WXCtr.saveImge(GameCtr.friendsCircleImg, () => {
            this.ndSaveTip.active = true;
        });
    }

    showSaveTip(isShow) {
        this.ndSaveTip.active = isShow;
    }

    clickSaveBtn() {
        this.showSaveTip(false);
        this.sprExtension.node.active = false;
        GameCtr.getFriendsCircleGold();
    }

    //广告
    showVideoAd() {
        WXCtr.showVideoAd();
    }

    //分享完成提示
    showShareGroupTip(tipString = null) {
        let lb = this.ndTip.getComponent(cc.Label);
        if (tipString) {
            lb.string = tipString;
        } else {
            lb.string = "亲，只有分享到群才能接着玩哦！";
        }
        this.ndTip.stopAllActions();
        this.ndTip.runAction(cc.sequence(
            cc.fadeIn(0),
            cc.delayTime(2.0),
            cc.fadeOut(2.0),
        ));
    }

    //跳过
    cancel() {
        GameCtr.gameReviveData = [];
        GameCtr.showRevive(false);
    }

    // update (dt) {}
}
